﻿
using AntdUI;
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using DataCollection.WinForm.ViewModel;
using Mapster;

namespace DataCollection.WinForm.Forms
{
    public partial class ManageProjectForm : DCBaseForm
    {
        #region 属性

        private ProjectViewModel viewModel;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        /// <summary>
        /// 类型 0-工程管理 1-工程选择
        /// </summary>
        public int Type;

        private ProjectItem? SelectProjectItem;

        #endregion

        #region 自定义方法
        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            ListTable.Height = Height - Header.Height - DCFlowPanel1.Height - Divider1.Height - Divider1.Height - Pagination1.Height - 5;
            ListTable.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","工程编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","工程名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("StakeMark","工程桩号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeSpecification","管材规格",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeLength","管道长度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeNature","管道性质",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("TestType","试压类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Company","施工单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerPrecision","压力表精度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerRange", "压力表量程", AntdUI.ColumnAlign.Center)
            };
            if (this.Type == 0)
            {
                Header.Text = "管理试压工程";
            }
            else if (this.Type == 1)
            {
                Header.Text = "选择试压工程";
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            List<ProjectModel> list = ProjectModel.ListPage(where, PageNumber, PageSize, ref TotalPages);
            List<ProjectItem> itemList = new List<ProjectItem>();
            foreach (ProjectModel model in list)
            {
                ProjectItem item = new ProjectItem();
                model.Adapt(item);
                itemList.Add(item);
            }
            ListTable.DataSource = itemList;
        }
        #endregion

        #region 系统事件
        public ManageProjectForm()
        {
            InitializeComponent();
            Header.Text = "试压工程管理";
            FullScreen = true;
        }

        private void ManageProject_Load(object sender, EventArgs e)
        {
            SetupForm();
            DCSpin(this, "加载数据...", (action) =>
            {
                LoadList();
            }, null, () => { });
        }
        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }
        private void AddButton_Click(object sender, EventArgs e)
        {
            AddProjectForm frm = new AddProjectForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            if (SelectProjectItem == null)
            {
                Error("请选择工程");
                return;
            }
            string msg = string.Format("您确定要删除【{0}】工程吗？", SelectProjectItem.Name);

            AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
            {
                OnOk = config =>
                {
                    viewModel = new ProjectViewModel(long.Parse(SelectProjectItem.Number));
                    Response response = viewModel.Delete();
                    if (response.Code == Response.SuccessCode)
                    {
                        Success(response.Msg);
                        LoadList();
                        SelectProjectItem = null;
                    }
                    else
                    {
                        Error(response.Msg);
                    }
                    return true;
                },
            });
        }

        private void ListTable_CellClick(object sender, AntdUI.TableClickEventArgs e)
        {
            if (e.Record is ProjectItem data)
            {
                SelectProjectItem = data;
            }
        }

        private void ListTable_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (Type == 1)
            {
                if (e.Record is ProjectItem data)
                {
                    string msg = string.Format("您确定选择【{0}】工程吗？", data.Name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (this.Owner is not null)
                            {
                                if (this.Owner is IndexForm)
                                {
                                    IndexForm? form = (IndexForm)this.Owner;
                                    form.SelectProjectItem = data;
                                    form.LoadProjectInfo();
                                }
                                else if (this.Owner is ReportForm2)
                                {
                                    ReportForm2? form = (ReportForm2)this.Owner;
                                    form.SelectedProjectItem = data;
                                    form.LoadProjectInfo();
                                }
                                this.Invoke(new Action(() =>
                                {
                                    this.Close();
                                }));
                            }

                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
